package com.travel.controller;

import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.travel.entity.HotelOrder;
import com.travel.entity.ScenicOrder;
import com.travel.service.HotelOrderService;
import com.travel.service.ScenicOrderService;
import com.travel.service.impl.HotelOrderServiceImpl;
import com.travel.service.impl.ScenicOrderServiceImpl;
import com.travel.util.PageInfo;

/**
 * Servlet implementation class ScenicOrderServlet
 */
@WebServlet("/OrderHotelServlet")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HotelOrderService hotelOrderService = new HotelOrderServiceImpl();
	private ScenicOrderService scenicOrderService = new ScenicOrderServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OrderServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String op = request.getParameter("op");
		if ("searchByPageHotelOrder".equals(op)) {
			// 分页查询列表
			doFindHotelOrderByPage(request, response);
		} else if ("deleteHotel".equals(op)) {
			doDeleteHotel(request, response);
		}
		if ("searchByPageScenicOrder".equals(op)) {
			doFindHotelScenicByPage(request, response);
		}else if ("deleteScenic".equals(op)) {
			doDeleteScenic(request, response);
		}
	}

	private void doDeleteScenic(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String scenicOrderId = request.getParameter("scenicOrderId");
		scenicOrderService.delete(Integer.parseInt(scenicOrderId));
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		String userName = request.getParameter("userName");

		PageInfo<ScenicOrder> findByPage = scenicOrderService.findByPage(pageNum, 3, userName);
		System.out.println(findByPage);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(findByPage);
		System.out.println(json);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doDeleteHotel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String hotelOrderId = request.getParameter("hotelOrderId");
		hotelOrderService.deletById(Integer.parseInt(hotelOrderId));
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		String userName = request.getParameter("userName");

		PageInfo<HotelOrder> findByPage = hotelOrderService.getUserIdByPage(pageNum, 3, userName);
		System.out.println(findByPage);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(findByPage);
		System.out.println(json);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);

	}

	private void doFindHotelScenicByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		String userName = request.getParameter("userName");

		PageInfo<ScenicOrder> findByPage = scenicOrderService.findByPage(pageNum, 3, userName);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(findByPage);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);

	}

	private void doFindHotelOrderByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		//String userName = "u345";
		String userName = request.getParameter("userName");

		PageInfo<HotelOrder> findByPage = hotelOrderService.getUserIdByPage(pageNum, 3, userName);
		System.out.println(findByPage);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(findByPage);
		System.out.println(json);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
	}

}
